package com.wanjia.estate.service.bdservice.sjdspireservice;

import com.wanjia.estate.pojo.entity.Sjd;
import com.wanjia.estate.properties.FileTemplateProperties;
import com.wanjia.estate.utils.MD5Util;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


@Service
public class ExcelReaderWriterSjdService {

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    public String ExcelReaderWriter(Sjd sjd,String folder) {
        String md5FileName = MD5Util.getMD5(sjd.getSjbh());
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getZjbdcsjdpath());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            Row row3 = sheet.getRow(2);
            row3.getCell(6).setCellValue(sjd.getSjbh());

            Row row4 = sheet.getRow(3);
            row4.getCell(1).setCellValue(sjd.getSqr());

            Row row5 = sheet.getRow(4);
            row5.getCell(1).setCellValue(sjd.getYwr());

            Row row6 = sheet.getRow(5);
            row6.getCell(2).setCellValue(sjd.getRq());
            row6.getCell(6).setCellValue(sjd.getZl());

            Row row7 = sheet.getRow(6);
            row7.getCell(1).setCellValue(sjd.getBdcsl());

            Row row8 = sheet.getRow(7);
            row8.getCell(0).setCellValue(sjd.getGzr());
            row8.getCell(5).setCellValue(sjd.getFzck());

            Row row20 = sheet.getRow(19);
//            row16.getCell(1).setCellValue(sjd.getSjr());
//            row16.getCell(3).setCellValue(sjd.getSjjg());
            row20.getCell(6).setCellValue(sjd.getSjrq());

//            Row row18 = sheet.getRow(17);
//            row18.getCell(1).setCellValue(sjd.getZslx());

            Row row23 = sheet.getRow(22);
            row23.getCell(2).setCellValue(sjd.getSqrlzr());


            FileOutputStream outputStream = new FileOutputStream(folder+"/"+md5FileName + "sjd.xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+md5FileName + "sjd.xlsx");
        return file.getPath();
    }
}
